var winWidth;
var winHeight;
var iframeWidth;
var iframeHeight;

$(function(){
    var timeOutId = null;

    onResize();

    $(window).resize(onResize);
    hideIframe();

    $("iframe").load(function(){
        showIframe();
    });
    $("iframe").unload(function(){
        hideIframe();
    });

    if(location.hash==""){
        $("iframe").attr("src","/login");
        location.hash="";
    }else{
        $("iframe").attr("src",location.href.replace("#",""));
    }

    function hideIframe(){
        $("iframe").hide();
        timeOutId = setTimeout(showIframe,3000);
    }

    function showIframe(){
        clearTimeout(timeOutId);
        timeOutId = null;
        $("iframe").show();
    }
    
    if(location.hash==""){
    	$("iframe").attr("src","/login");
    	location.hash="";
    }else{
    	$("iframe").attr("src",location.href.replace("#",""));
    }

    function onResize() {
        winWidth = Math.max($(window).width(),150);
        winHeight = Math.max($(window).height(),60);

        iframeWidth = winWidth-12;
        iframeHeight = winHeight-12;

        setBg();
        setIframe();
    }

    function setBg(){
        $(".mainBody").css("width",winWidth+"px");
        $(".mainBody").css("height",winHeight+"px");
    }

    function setIframe(){
        $("iframe").css("width",iframeWidth+"px");
        $("iframe").css("height",iframeHeight+"px");
    }
});